﻿(function ($) {
    var defaults = {
        fileName: '',//附件名字
        fileId: 0,//附件Id
        fileType: 0,//问题类型0 无1 课件 2 作业
        teacherImg: "http://img0.bdstatic.com/img/image/936622762d0f703918f643560e5533d269759eec43d.jpg",//老师用户图像
        teacherName: "张三老师",//老师用户名
        teacherLink: "javascript:void(0)",//老师用户展示页链接
        teacherId: 0//班级圈的老师Id
    };

    var getLength = function (str) {
        return Math.ceil(str.replace(/^\s+|\s+$/ig, '').replace(/[^\x00-\xff]/ig, 'xx').length / 2);
    };
    var subStr = function (str, len) {
        if (!str) {
            return '';
        }
        len = len > 0 ? len * 2 : 280;
        var count = 0, //计数：中文2字节，英文1字节
            temp = '';  //临时字符串
        for (var i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 255) {
                count += 2;
            }
            else {
                count++;
            }
            //如
            if (count > len) {
                return temp;
            }
            //将当前内容加到临时字符串
            temp += str.charAt(i);
        }
        return str;
    };
    //答疑弹出框标题输入字数控制 start
    $('.titBox input').live("keyup",function () {
        var self = $(this);
        var inputNum = 20 - getLength(self.val());
        if (inputNum < 0) {
            self.val(subStr(this.value, 20));
        } else {
            self.next().text(inputNum);
        }
    });
    //答疑弹出框标题输入字数控制 end
    $.QueryBox = function (options) {
        var url = location.protocol + "//" + location.host + "/Upload/UploadFiles";
        var strHtml = '<div id="con"><div class="zRoomBox" style="width: 609px;"><div class="zRoomBox_tit"><h3><strong>我有疑问</strong></h3></div><div class="zRoomBox_con"><div class="r_playBoxQuestion"><ul class="clearfix"><li><div class="lt">问：</div><div class="mt"><span class="pro_name">'
        strHtml += '</span><dl class="pel_name"><dt><a href="' + options.teacherLink + '"><img src="' + options.teacherImg + '"></a></dt><dd><a href="' + options.teacherLink + '">' + options.teacherName + '</a></dd></dl></div></li><li class="one"><div class="lt"><span class="star">*</span>';
        strHtml += '标题：</div><div class="mt"><div class="titBox"><input id="faqTitle" type="text" style="color:black" /><span>20</span></div></div><div class="rt"></div></li><li class="two"><div class="lt"><span class="star">*</span>描述：</div><div class="mt"><div class="describeBox" id="counterDiv1" style="padding: 0; overflow: hidden;"><textarea id="Content" cols="20" rows="2" ></textarea></div></div><div id="tishi" class="rt"><span class="iconWarm" style="display:none"></span><span class="vm" style="display:none">标题与描述都不能为空！</span></div></li><li class="three"><div class="submitFalse">'
        strHtml += '<input id="submitInfo" class="sub" type="submit" value="提交"><a href="#" class="false" onclick="$.fancybox.close();">取消</a></div></li></ul></div></div><a class="zRoomBox_close" href="javascript:;" onclick="$.fancybox.close();"></a></div></div>	';
        var editor;
        $.fancybox(strHtml, {
            padding: 0,
            scrolling: 'visible',
            closeBtn: false,
            modal: true,
            afterShow: function () {
                editor = $('#Content').xheditor({
                    plugins: TsingDa.VoicePlugin,
                    tools: 'Fontface,FontSize,FontColor,Bold,Align,List,Emot,|,Link,Img,Fullscreen,Source,About',//REC,Sound, 去除录音和上传音频
                    width: 437,
                    height: 198,
                    upImgUrl: url + "?type=images",
                    upImgExt: "jpg,jpeg,gif,png",
                    upLinkUrl: url + "?type=attachment",
                    upLinkExt: "zip,rar,txt",
                    onUpload: function (arrMsg) {
                    }
                });
            }
        });
        //$("#faqTitle").keydown(function () {
        //    var SetLength = 40;//字符长度设置为40
        //    if (len($(this).val()) > SetLength) {
        //        var str = $(this).val().substr(0, SetLength);
        //        while (len(str) > SetLength) {
        //            str = str.substr(0, str.length - 1);
        //        }
        //        $(this).attr("value", str);
        //        //alert("输入长度不能超过20个汉字或40个字符");
        //    }
        //});
        //var editor = $('#Content').xheditor({ tools: 'full', width: 500, height: 200 });      
        //editor.settings.focus = function () {
        //    var SetLength = 10;//字符长度设置为1000 
        //    var str_source = editor.getSource();
        //    if (len(str_source) > SetLength) {
        //        editor.setSource("");//清空上一次调用时的内容
        //        str_source = str_source.substr(0, SetLength);
        //        while (len(str_source) > SetLength) {
        //            str_source = str_source.substr(0, str.length - 1);
        //        }
        //        editor.setSource(str_source);
        //        //editor.pasteHTML(str_source + "</br>");                
        //        //    alert("输入长度不能超过500个汉字或1000个字符");
        //    }
        //}

        //判断输入字符长度
        //function len(s) {
        //    var l = 0;
        //    var a = s.split("");
        //    for (var i = 0; i < a.length; i++) {
        //        if (a[i].charCodeAt(0) < 299) {
        //            l++;
        //        }
        //        else {
        //            l += 2;
        //        }
        //    } return l;
        //}
        $("#submitInfo").bind("click", function () {
            var editor = $('#Content').xheditor({ tools: 'full', width: 500, height: 200 });
            if ($("#faqTitle").val() != "" && editor.getSource().length > 0) {
                $.post("/FAQ/AddFAQInfo", { TeacherID: options.teacherId, Title: $("#faqTitle").val(), Description: $("#Content").val(), RelatedType: options.fileType, RelatedID: options.fileId }, function (data) {
                    if (data.code == "1") {
                        TsingdaTips.alert("提问成功！", function () {
                            $.fancybox.close();
                            location.href = location.href;
                        });
                    }
                    else {
                        TsingdaTips.alert("提问失败！");
                    }
                })
            }
            else {
                //TsingdaTips.alert("标题与描述都不能为空！");
                $("#tishi").children().show().next().show();
            }
        });
    }
})(jQuery);